﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;

namespace Hardanger.View
{
	/// <summary>
	/// Interaction logic for ResultsDetails.xaml
	/// </summary>
	public partial class ResultsDetails : Window
	{
		public ResultsDetails()
		{
			InitializeComponent();
		}

        private void Save_Response_Click(object sender, RoutedEventArgs e)
        {
            WriteFile(PromptUserForFileName(), ResponseText.Text);

        }

        private void Save_Request_Click(object sender, RoutedEventArgs e)
        {
            WriteFile(PromptUserForFileName(), RequestText.Text);
        }

        private string PromptUserForFileName()
        {
            var sfd = new SaveFileDialog { Filter = "*.txt|*.*" };
            var result = sfd.ShowDialog();
            if (result.HasValue && result.Value)
                return sfd.FileName;
            else
                return "";
        }

        private void WriteFile(string fileName, string content)
        {
            if (String.IsNullOrEmpty(fileName))
                return;

            using (var sw = new StreamWriter(fileName))
            {
                try
                {
                    sw.Write(content);
                }
                catch (Exception e)
                {
                    MessageBox.Show(string.Format("There was an error writing to the file {0}\r\nMessage: {1}", fileName, e.Message));
                }
            }
        }
	}
}
